package cn.chuxing.config.security;

import cn.chuxing.config.security.JwtUserFactory;
import cn.chuxing.dao.model.PlUser;
import cn.chuxing.rpc.api.PlUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * Created by cooper on 2018/1/5.
 */
@Component
public class JwtUserDetailService implements UserDetailsService {

    @Autowired
    private PlUserService userService;

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {

        PlUser userDTO = userService.selectByPrimaryKey(1);
        if (userDTO.getUserId() == null){
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", name));
        }else {
            return JwtUserFactory.create(userDTO);
        }
    }
}
